package com.shareinfo.adminManage.auto.service.impl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.shareinfo.adminManage.auto.dao.TableDao;
import com.shareinfo.adminManage.auto.model.AutoDbModel;
import com.shareinfo.adminManage.auto.model.AutoDbTableFieldModel;
import com.shareinfo.adminManage.auto.model.AutoDbTableModel;
import com.shareinfo.adminManage.auto.service.TableService;
import com.shareinfo.auto.vo.ColumnInfo;
import com.shareinfo.auto.vo.ResultInfo;
import com.shareinfo.auto.vo.TableInfo;

/**
 * 
 * @author Administrator
 * 
 */
@Service("tableService")
public class TableServiceImpl implements TableService {

	@Resource(name = "tableDao")
	private TableDao tableDao;

	/**
	 * 判断数据库是否可以链接
	 */
	@Override
	public boolean ifCanConnection(){
		return tableDao.ifCanConnection();
	}
	
	@Override
	public void setAutoDbModel(AutoDbModel autoDbModel) {
		tableDao.setAutoDbModel(autoDbModel);
	}
	
	@Override
	public ResultInfo getAllTableList() {
		return tableDao.getAllTableList();
	}

	@Override
	public ResultInfo getTableInfo(String tableName) {
		return tableDao.getTableInfo(tableName);
	}

	@Override
	public ResultInfo getKeyColumn(String tableName) {
		return tableDao.getKeyColumn(tableName);
	}

	@Override
	public ResultInfo getColumns(String tableName) {
		return tableDao.getColumns(tableName);
	}
	
	/**
	 * 传入表对象，获取表AutoDbModel和List<ColumnInfo>对象
	 */
	public ResultInfo loadTable(String dbId,String tableName){
		ResultInfo resultInfo=getTableInfo(tableName);
		if(!resultInfo.isResult()){
			resultInfo.setMsg(tableName+"表不存在");
			return resultInfo;
		}
		//一、初始化设置表信息
		AutoDbTableModel autoDbTableModel=new AutoDbTableModel();
		autoDbTableModel.setDbId(dbId);
		autoDbTableModel.setTableName(tableName);
		//设置表的注释
		if(StringUtils.isEmpty(resultInfo.getObj().toString())){
			autoDbTableModel.setTableComments(tableName);
		}else{
			autoDbTableModel.setTableComments(resultInfo.getObj().toString());
		}
		//二、获取主键
		resultInfo=getKeyColumn(tableName);
		if(!resultInfo.isResult()){
			resultInfo.setMsg(resultInfo.getMsg());
			return resultInfo;
		}
		//设置主键数、主键名称。多主键名称中间是用_分隔
		List<String> keyColumnList=(List<String>)resultInfo.getObj();
		autoDbTableModel.setTablePkNum(keyColumnList.size());
		String tablePkValue="";
		for (int i = 0; i < keyColumnList.size(); i++) {
			tablePkValue+=keyColumnList.get(i);
			if(i!=keyColumnList.size()-1)
				tablePkValue+="_";
		}
		autoDbTableModel.setTablePkName(tablePkValue);
		//三、获取表字段
		resultInfo=getColumns(tableName);
		if(!resultInfo.isResult()){
			resultInfo.setMsg(resultInfo.getMsg());
			return resultInfo;
		}
		//字段列表
		List<ColumnInfo> listColumn = (List<ColumnInfo>)resultInfo.getObj();
		ColumnInfo columnInfo=null;
		List<AutoDbTableFieldModel> list = new ArrayList<AutoDbTableFieldModel>();
		AutoDbTableFieldModel autoDbTableFieldModel=null;
		for (int i = 0; i < listColumn.size(); i++) {
			autoDbTableFieldModel=new AutoDbTableFieldModel();
			columnInfo=listColumn.get(i);
			autoDbTableFieldModel.setDbTableId(autoDbTableModel.getId());
			autoDbTableFieldModel.setSqlColumnName(columnInfo.getSqlColumnName());
			autoDbTableFieldModel.setSqlDataType(columnInfo.getSqlDataType()+"");
			autoDbTableFieldModel.setSqlColumnSize(columnInfo.getSqlColumnSize()+"");
			autoDbTableFieldModel.setSqlDecimalDigits(columnInfo.getSqlDecimalDigits()+"");
			autoDbTableFieldModel.setSqlNullAble(columnInfo.getSqlNullAble()+"");
			autoDbTableFieldModel.setSqlRemarks(columnInfo.getSqlRemarks());
			autoDbTableFieldModel.setSqlColumnDef(columnInfo.getSqlColumnDef());
			autoDbTableFieldModel.setClassColumnName(columnInfo.getClassColumnName());
			autoDbTableFieldModel.setClassDataType(columnInfo.getClassDataType());
			//autoDbTableFieldService.save(autoDbTableFieldModel);
			list.add(autoDbTableFieldModel);
		}
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("autoDbTableModel", autoDbTableModel);
		map.put("list", list);
		resultInfo.setObj(map);
		return resultInfo;
	}
	
}
